/*
 * @Description: 页面跳转
 * @Author: knight
 * @Date: 2022-12-19 15:51:35
 * @LastEditTime: 2025-08-04 10:51:37
 * @LastEditors: ZhangHan
 */
import { isString } from "@/utils/is";

type Option = {
  url: string;
  complete?: (res: UniApp.GeneralCallbackResult) => void;
  fail?: (res: UniApp.GeneralCallbackResult) => void;
  success?: (res: UniApp.GeneralCallbackResult) => void;
};

function handleError(e: UniApp.GeneralCallbackResult) {
  console.error(e);
}

// 页面跳转
export function useGo() {
  function go(opt: string | Option, isRedirect = false) {
    if (!opt) {
      return;
    }

    if (isString(opt)) {
      isRedirect
        ? uni.redirectTo({ url: opt, fail: handleError })
        : uni.navigateTo({ url: opt, fail: handleError });
    } else {
      isRedirect ? uni.redirectTo(opt) : uni.navigateTo(opt);
    }
  }

  return go;
}

// 跳转到 tabBar 页面
export function useSwitch() {
  function switchTab(opt: string | Option) {
    if (!opt) {
      return;
    }

    if (isString(opt)) {
      uni.switchTab({ url: opt, fail: handleError });
    } else {
      uni.switchTab(opt);
    }
  }

  return switchTab;
}
